package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/models/mysql"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *doctorrpc.LoginRequest) (*doctorrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var doctor mysql.Doctor
	//:= &mysql.Doctor{
	//	Model:      gorm.Model{},
	//	DoctorCode: in.DoctorCode,
	//	Password:   in.Password,
	//}

	if err := doctor.FindDoctor(mysql.DB, in.DoctorCode); err != nil {
		return nil, err
	}

	if doctor.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	if in.Password != doctor.Password {
		return nil, errors.New("密码错误")
	}

	return &doctorrpc.LoginResponse{
		DoctorId:   int64(doctor.ID),
		DoctorName: doctor.DoctorName,
		DoctorRoom: doctor.DoctorRoom,
	}, nil
}
